package com.etc.zzsys.dao.impl;

import java.util.List;

import com.etc.zzsys.dao.UserDao;
import com.etc.zzsys.entity.Question;
import com.etc.zzsys.entity.User;
import com.etc.zzsys.util.BaseDao;
import com.etc.zzsys.util.PageData;

public class UserDaoImpl implements UserDao{

	@Override
	public List<User> getUsers(){
		return (List<User>)BaseDao.select("select * from user",User.class);
	}

	@Override
	public boolean addUser(User user) {
		return BaseDao.execute("INSERT INTO user(userName,userPwd, userSex, userTel, userAddress, userLevel, userState) VALUES (?,?,?,?,?,?,?)", user.getUserName(),user.getUserPwd(),user.getUserSex(),user.getUserTel(),user.getUserAddress(),user.getUserLevel(),user.getUserState())>0;
	}

	@Override
	public boolean delUser(int userId) {
		// TODO Auto-generated method stub
		return BaseDao.execute("delete from user where userId=?",userId)>0;
	}

	@Override
	public boolean updateUser(User u) {
		// TODO Auto-generated method stub
		String sql="update user set userName=?,userSex=?,userTel=?,userAddress=?,userLevel=?,userState=? where userId=?";
		return BaseDao.execute(sql,u.getUserName(),u.getUserSex(),u.getUserTel(),u.getUserAddress(),u.getUserLevel(),u.getUserState(),u.getUserId())>0;
	}

	@Override
	public User login(String username, String pwd) {
		// TODO Auto-generated method stub
		String sql="select * from user where userName=? and userPwd=?";
		List<User> list=(List<User>)BaseDao.select(sql,User.class,username,pwd);
		if(list.size()>0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public PageData<User> queryQuestionByPage(int page, int pageSize, String key) {
		// TODO Auto-generated method stub
		return BaseDao.getPage("select * from user where username like ?", page, pageSize, User.class,"%"+key+"%");
	}

	@Override
	public User getUserByName(String username) {
		// TODO Auto-generated method stub
		List<User> list=(List<User>)BaseDao.select("select * from user where username=?", User.class,username);
		if(list.size()>0) {
			return list.get(0);
		}
		return null;
		
	}

	@Override
	public boolean regist(User u) {
		// TODO Auto-generated method stub
		return BaseDao.execute("insert into user(userName,userPwd,userSex,userTel,userAddress,userLevel,userState) values(?,?,?,?,?,1,0)",u.getUserName(),u.getUserPwd(),u.getUserSex(),u.getUserTel(),u.getUserAddress())>0;
	}
	
	
}
